package com.zy.springboot.filter;

import com.zy.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author zhaoyi
 * @date 2021/9/22 11:04
 * https://www.cnblogs.com/muxi0407/p/11950627.html
 * 客户端 -> 过滤器1 -> 过滤器2 -> servlet -> 过滤器2 -> 过滤器1 -> 客户端 链式流转
 */
@Order(1)
@WebFilter("/user/*")
public class LoggingFilter implements Filter {

    @Autowired
    private UserService userService;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        System.out.println("Filter userService is  " + userService);

        HttpServletRequest req = (HttpServletRequest) servletRequest;
        String url = req.getRequestURI();
        System.out.println("url is " + url);
        if (url != null && url.contains("login")) {
            //登录也不拦截
            // filter的链式调用；将请求转给下一条过滤链
            filterChain.doFilter(servletRequest, servletResponse);
            System.out.println("执行完成返回了....");
            return;
        } else {
            //未进行链式调用 就是不了到此结束
            System.out.println("假设未登录");
        }


    }
}
